<template>
	<view class="chart-wrapper">
		<!-- 自定义标题区域 -->
		<view class="chart-title">
			<text class="title-text">营销转化分析</text>
		</view>

		<qiun-data-charts type="funnel" :opts="opts" :chartData="chartData" />
	</view>
</template>

<script>
	import {
		selectOpenDoorConversion
	} from '@/common/api/statistics.js'
	import {
		convertObjToArray
	} from '@/utils/util.js'
	export default {
		data() {
			return {
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['funnel'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					padding: [15, 15, 0, 15],
					enableScroll: false,
					extra: {
						funnel: {
							activeOpacity: 0.3,
							activeWidth: 10,
							border: true,
							borderWidth: 2,
							borderColor: "#FFFFFF",
							fillOpacity: 1,
							labelAlign: "right",
							linearType: "custom",
							// minSize: 20
						}
					}
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			async getData() {
				try {
					let res = await selectOpenDoorConversion()
					this.getServerData(res.data);
				} catch (error) {
					//TODO handle the exception
				}
			},
			getServerData(data) {
				console.log(this.convertToDataFormat(data,true), '-----111---')
				let res = {
					series: [{
						data: this.convertToDataFormat(data,false)
					}]
				};
				this.chartData = JSON.parse(JSON.stringify(res));
			},
			convertToDataFormat(obj, sortAsc = true) {
				// 先转换为目标格式数组
				const result = Object.entries(obj).map(([key, value]) => ({
					name: key,
					centerText: `${key}:${value}`,
					value: value
				}));

				// 按value排序（sortAsc为true时从小到大，false时从大到小）
				result.sort((a, b) => {
					return sortAsc ? a.value - b.value : b.value - a.value;
				});

				return result;
			}
		}
	};
</script>
<style scoped>
	.chart-wrapper {
		padding: 30rpx;
		background-color: #fff;
		/* border-radius: 16rpx; */
		margin-bottom: 24rpx;
	}

	/* 自定义标题样式 */
	.chart-title {
		display: flex;
		justify-content: space-between;
		/* 标题左对齐，省略号右对齐 */
		align-items: center;
		padding: 0 20rpx 20rpx;
		/* 底部留出距离 */
		/* border-bottom: 1px solid #f5f5f5; */
		/* 底部分隔线 */
		/* margin-bottom: 20rpx; */
	}

	.title-text {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
		/* 标题深色 */
	}

	.more-icon {
		font-size: 36rpx;
		color: #999;
		/* 省略号浅色 */
		padding: 10rpx;
	}
</style>